import {
	mapState,
	mapMutations
} from 'vuex'
import * as util from './BaseUtil.js'
export default {
	computed: {
		...mapState(['totalSeconds', 'totalDuration', 'playSeconds', 'playDuration', 'progress', 'playState',
			'playMode', 'musicIsReady', 'playList', 'playIndex', 'lrcStr', 'lrcs', 'lrc', 'recentlyPlayList'
		]),
		...mapState(['paramSongList', 'isFirstRun', 'showPlayBar'])
	},
	methods: {
		...mapMutations(['setPlayIndex', 'setTotalSeconds', 'setPlaySeconds', 'setProgress', 'setPlayState',
			'setPlayMode', 'setMusicIsReady', 'setPlayList', 'setLrcStr', 'setLrcs', 'setLrc', 'setRecentlyPlayList'
		]),
		...mapMutations(['setParamSongList', 'setIsFirstRun', 'setShowPlayBar']),
		like(music) {
			this.songListService.like(music)
			this.$forceUpdate();
		},
		unLike(music) {
			this.songListService.unLike(music);
			this.$forceUpdate();
		},
		isLike(music) {
			return this.musicService.isLike(music);
		},
		controllerPlay(type) {
			if (type == 'play') {
				if (this.playState) {
					this.pause();
				} else {
					if (this.isFirstRun) {
						const progress = {
							progress: this.progress,
							playSeconds: this.playSeconds
						}
						this.setMusic(this.playIndex, progress, () => {
							this.play();
						})
					} else {
						this.play();
					}
				}
			} else if (type == 'next') {
				this.next();
			} else if (type == 'prev') {
				this.prev();
			}
		},
		playMusicByMusic(music) {
			let playList = this.playList;
			const index = util.findIndex(playList, music);
			let playIndex = null;
			if (index == -1) {
				playList.push(music);
				this.setPlayList(playList);
				playIndex = playList.length - 1;

			} else {
				playIndex = index;
			}
			this.setMusic(playIndex, null, () => {
				this.play();
			})
		},
		nextPlay(music) {
			const playIndex = this.playIndex;
			let playList = this.playList;
			const musicIndex = util.findIndex(playList, music);
			if (musicIndex != -1) {
				if (playList.length - 1 == playIndex) {
					playList.push(music);
				} else {
					let index = playIndex + 1;
					playList[musicIndex] = playList.splice(index, 1, playList[musicIndex])[0];
				}

			} else {
				const index = playIndex + 1;
				playList.splice(index, 0, music);
			}
			this.setPlayList(playList);

		}
	}
}
